let joblistModule = (function () {
  let $tableBox = $(".tableBox"),
    $tbody = $tableBox.find('tbody');

  async function bindHTML() {
    let result = await axios.get("/job/list");
    let data = result.data;
    let str = '';
    data.forEach(item => {
      let { id,
        name,
        desc,
        power } = item;
      console.log(power)
      // 员工操作权|部门操作权|职务操作权|部门全部客户|公司全部客户|重置密码
      str += `
        <tr>
          <td class="w8">${id}</td>
          <td class="w10">${name}</td>
          <td class="w20">${desc}</td>`;
      // 有bug，需要用正则来匹配（如果多个权限他顺序不一样就无法判断了）
      if (power === 'userhandle|departhandle|jobhandle|customerall' || power === 'userhandle|departhandle|jobhandle|departcustomer|customerall|resetpassword') {
        str += `<td class="w50">员工操作权|部门操作权|职务操作权|部门全部客户|公司全部客户|重置密码</td>`;
      } else if (power === 'customerall') {
        str += `<td class="w50">部门全部客户|公司全部客户</td>`;
      } else if (power === 'customermy') {
        str += `<td class="w50">部门全部客户</td>`;
      } else if (power === 'userhandle|departhandle|jobhandle') {
        str += `<td class="w50">员工操作权|部门操作权|职务操作权</td>`;
      } else if (power === 'userhandle') {
        str += `<td class="w50">员工操作权</td>`;
      } else {
        str += `<td class="w50">无任何权限</td>`;
      }
      str += `
          <td class="w12" jobId="${id}">
            <a href="javascript:;">编辑</a>
            <a href="javascript:;">删除</a>
          </td>
        </tr>
      `;
    })
    $tbody.html(str);
    // console.log(result)
  }
  function editDeleHandle(ev) {
    $tbody.click(async ev => {
      let target = ev.target,
        $target = $(target),
        TAG = target.tagName,
        TEXT = target.innerText,
        jobId = $target.parent().attr("jobId");
      if (TAG === 'A') {
        if (TEXT === '编辑') {
          window.location.href = `jobadd.html?jobId=${jobId}`;
          return;
        }
        if (TEXT === '删除') {
          let flag = confirm(`确定删除编号为${jobId}的职位吗！`)
          if (flag) {
            let result = await axios.get("/job/delete", { params: { jobId } });
            if (result.code !== 0) {
              console.log('删除失败，请重试！');
              return;
            }
            bindHTML();
            return;
          }

        }
      }
    })
  }
  return {
    init() {
      bindHTML();
      editDeleHandle();
    }
  }
})();
joblistModule.init();